package top.lixunda.books3.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.books3.dao.SlideMapper;
import top.lixunda.books3.entity.Slide;
import top.lixunda.books3.entity.SlideExample;
import top.lixunda.books3.service.SlideService;
import top.lixunda.books3.util.Log;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SlideServiceImpl implements SlideService {

    @Autowired
    private SlideMapper slideMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public List<Slide> selectUsingSlides() {
        SlideExample slideExample = new SlideExample();
        SlideExample.Criteria criteria = slideExample.createCriteria();
        criteria.andAbleEqualTo((byte) 1);
        return dealSlidesToFormat(slideMapper.selectByExample(slideExample));
    }

    private List<Slide> dealSlidesToFormat(List<Slide> list){
        String contextPath = request.getContextPath();
        Log.d("----------> 在 SlideServiceImpl.dealSlidesToFormat 中获取到contextPath 为 "+contextPath);
        for(Slide slide : list){
            String imagePath = slide.getImage();
            if(!imagePath.startsWith("http")){
                imagePath = contextPath + (imagePath.startsWith("/") ? imagePath : "/"+imagePath);
                slide.setImage(imagePath);
            }
            Log.d("------------>处理 SlideImage 路径 ： "+imagePath);
        }
        return list;
    }
}
